<template>
  <div class="register_wrap">
    <div class="register_zhuce">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model.trim="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <!-- <el-input v-model="form.password"></el-input> -->
          <el-input
            placeholder="请输入密码"
            v-model.trim="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <!-- <el-input v-model="form.repeate_password"></el-input> -->
          <el-input
            placeholder="请输入密码"
            v-model.trim="form.repeate_password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :plain="true" @click="onRegister"
            >注册</el-button
          >
          <el-button type="primary" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Message } from "element-ui";
// import request from "@/api/request.js";
import { req_register } from "@/api/registerLogin.js";
export default {
  data() {
    return {
      form: {
        name: "",
        password: "",
        repeate_password: "",
      },
    };
  },
  methods: {
    async onRegister() {
      console.log("给变量或者函数起名字一定要有含义");
      // 收集到输入的用户名和密码

      console.log(
        this.form.name,
        this.form.password,
        this.form.repeate_password
      );
      // 收集到用户名和密码之间将密码和重复密码进行对比，是否一致 一致发送注册请求 不一致 清空请重新输入
      if (this.form.password === this.form.repeate_password) {
        console.log("发送ajax请求");
        // 封装好的ajax请求（axios）的文件 一般文件的名称network  api  utils
        // 回忆request出入四个参数method,url,arg,headerType  返回值是一个promise对象  异异步微任务  就可以通过async await 去解决异步问题 变为同步
        /* let result = await request(
          "post",
          "/register",
          {
            user_name: this.form.name,
            password: this.form.password,
          },
          "form"
        ); */
        // 目的 如果是四个参数比较占用变量，将其变为一个对象
        /* let result = await request({
          method: "post",
          url: "/register",
          arg: {
            user_name: this.form.name,
            password: this.form.password,
          },
          headerType: "form",
        }); */
        let result = await req_register({
          method: "post",
          url: "/register",
          arg: {
            user_name: this.form.name,
            password: this.form.password,
          },
          headerType: "form",
        });

        console.log(result);
        // 注册之后跳转到登录页面
        this.$router.push({
          name: "login",
        });
      } else {
        // console.log("请重新输入");
        // this.$message("这是一条消息提示");
        Message({
          type: "warning",
          message: "请重新输入密码",
        });
        // 将密码和重复密码的输入空清空
        this.form.password = "";
        this.form.repeate_password = "";
      }
    },
    onReset() {
      console.log("重置");
      // 重置的意思就是让输入都为空
      this.form.name = "";
      this.form.password = "";
      this.form.repeate_password = "";
    },
  },
};
</script>

<style lang="scss" scoped>
.register_wrap {
  background-image: url("~@a/img/bggrey.jpg");
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .register_zhuce {
    // 目的：让注册框水平垂直居中
    // background-color: red;
    background-image: url("~@a/img/bggrey.jpg");
    background-size: 100% 100%;
    width: 400px;
    height: 400px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>